import sys
import os
import types
import inspect
from .core import Namespace
from . import target


#===============================================================================
# gabot module built-in utilities
#===============================================================================
class CustomParams(object):
    __slots__ = ["filename", "params"]
    def __init__(self, _filename, **_params):
        self.filename = _filename
        self.params = _params


def include(filename):
    """loads the given python file into the caller's namespace"""
    namespace = inspect.stack()[1][0].f_globals
    logging.loginfo("INCLUDE", "including %r", filename)
    execfile(filename, namespace)

def cond(condition, obj):
    """conditionally inserts a module into the inputs list"""
    if condition:
        return obj
    else:
        return None

def load(filename):
    """loads a python file, returns a module object; does not contaminate 
    the caller's namespace"""
    mod = types.ModuleType(os.path.basename(filename).replace(".", "_"))
    mod.__file__ = os.path.abspath(filename)
    mod.__dict__.update(common_namespace)
    logging.loginfo("LOAD", "loading %r", filename)
    execfile(filename, mod.__dict__)
    return mod


module_namespace = {
    #"core" : core,
    #"logging" : logging,
    #"backends" : backends,
    "Executable" : target.Executable,
    "SharedLib" : target.SharedLib,
    "Library" : target.Library,
    "Preprocessor" : target.Preprocessor,
    "Custom" : CustomParams,    
    "INCLUDE" : include,
    "LOAD" : load,
    "IF" : cond,
}


#===============================================================================
# platform-dependent defaults
#===============================================================================
params = Namespace()
env_list_splitter = ":"

def getenvlist(name):
    return tuple(os.environ.get(name, "").split(env_list_splitter))


def conf_win32():
    pass


def conf_posix():
    pass


if os.name == "nt":
    env_list_splitter = ";"
    conf_win32()
elif os.name == "posix":
    env_list_splitter = ":"
    conf_posix()
else:
    pass



















